﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Arbalest.Models;
using Arbalest.Web.Helpers;
using System.Data;
using System.Reflection;

namespace Arbalest.Web.Services
{
    public interface ICompanyRepository
    {
        IEnumerable<Company> List();
        Company Get(long id);

    }

    public class CompanyRepository : ICompanyRepository
    {
        #region ICompanyRepository Members

        public IEnumerable<Company> List()
        {
            List<Company> l = new List<Company>();

            DataSet ds = DataHelper.SqlServer.COREPROD.FetchDataSet(
                "SELECT * FROM test.Companies",
                CommandType.Text);
            
            // TODO: check for null ds;
            DataTable dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                Company newRec = new Company();
                foreach (PropertyInfo property in newRec.GetType().GetProperties())
                {
                    property.SetValue(newRec, dr[property.Name], property.GetIndexParameters());
                }

                #region Comment out
                
                //Warehouse src = ds.GetList<Warehouse>().Where(rec => rec.Id == userInput.Id).FirstOrDefault();
                //foreach (PropertyInfo property in src.GetType().GetProperties())
                //{
                //    if (property.GetIndexParameters().Length == 0)
                //    {
                //        property.SetValue(
                //            src,
                //            property.GetValue(userInput, null), 
                //            null);
                //    }
                //    else
                //    {
                //        property.SetValue(
                //            src,
                //            property.GetValue(userInput, property.GetIndexParameters()),
                //            property.GetIndexParameters());
                //    }
                //}
                #endregion Comment out

                l.Add(newRec);
            }

            return l;
        }

        public Company Get(long id)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    //public interface ISchoolUserRepository
    //{
    //    bool Add(Scofield.Web.Models.SchoolUser schoolUser);
    //    bool Update(Scofield.Web.Models.SchoolUser schoolUser);
    //    bool Delete(Scofield.Web.Models.SchoolUser schoolUser);

    //    Scofield.Web.Models.SchoolUser Get(int id);
    //    Scofield.Web.Models.School GetSchool(int schoolId);

    //    Scofield.Web.ViewModels.UserVolunteerWorkDetail GetWorkDetails(int id);
    //    IEnumerable<Scofield.Web.Models.SchoolUserDetail> GetSchoolUserDetails(int schoolId);
    //}

    //public class SchoolUserRepository : ISchoolUserRepository
    //{
    //    private Scofield.Web.Models.Scofield_Entities repo = new Scofield_Entities();

    //    public bool Add(SchoolUser schoolUser)
    //    {
    //        try
    //        {
    //            repo.AddToSchoolUsers(schoolUser);
    //            repo.SaveChanges();
    //            return true;
    //        }
    //        catch
    //        {
    //            return false;
    //        }
    //    }

    //    public bool Update(SchoolUser schoolUser)
    //    {
    //        try
    //        {
    //            SchoolUser buffer = repo.SchoolUsers.First(works => works.Id == schoolUser.Id);
    //            buffer = schoolUser;
    //            repo.SaveChanges();
    //            return true;
    //        }
    //        catch
    //        {
    //            return false;
    //        }
    //    }

    //    public bool Delete(SchoolUser schoolUser)
    //    {
    //        try
    //        {
    //            repo.DeleteObject(schoolUser);
    //            repo.SaveChanges();
    //            return true;
    //        }
    //        catch
    //        {
    //            return false;
    //        }
    //    }

    //    public SchoolUser Get(int id)
    //    {
    //        return repo.SchoolUsers.First(user => user.Id == id);
    //    }

    //    public School GetSchool(int schoolId)
    //    {
    //        return repo.Schools.First(school => school.Id == schoolId);
    //    }

    //    public Scofield.Web.ViewModels.UserVolunteerWorkDetail GetWorkDetails(int id)
    //    {
    //        repo.VolunteerWorkDetails.MergeOption = System.Data.Objects.MergeOption.PreserveChanges;
    //        Scofield.Web.ViewModels.UserVolunteerWorkDetail workDetails = new Scofield.Web.ViewModels.UserVolunteerWorkDetail();
    //        workDetails.SchoolUserDetail = repo.SchoolUserDetails.First(d => d.Id == id);
    //        workDetails.VolunteerWorks = repo.VolunteerWorkDetails.Where(d => d.SchoolUserId == id);
    //        return workDetails;
    //    }

    //    public IEnumerable<SchoolUserDetail> GetSchoolUserDetails(int schoolId)
    //    {
    //        return repo.SchoolUserDetails.Where(det => det.SchoolId == schoolId);
    //    }

    //}
}